package com.hyl.controlelr;

import com.hyl.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * LoginController
 *
 * @author hyl
 * @date 2025-06-30
 */

@Controller// 控制器
public class LoginController{

    @RequestMapping("/tologin")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView();
        view.setViewName("login"); //视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
        view.addObject("msg", "登录成功"); //数据存储到request域中
        return view;
    }

    @RequestMapping("/tologin2") //get/post请求都可以访问
    public ModelAndView login() {
        ModelAndView view = new ModelAndView();
        view.setViewName("login"); //视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
        view.addObject("msg", "登录成功"); //数据存储到request域中
        return view;
    }

    @RequestMapping(value = "/toreg",method = RequestMethod.GET) //必须是get
    public String toreg(Model model) {
        model.addAttribute("msg", "注册成功");
        return "login";//视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
    }


    @RequestMapping(value = "/reg",method = RequestMethod.GET) //必须是get
    public String reg(HttpServletRequest request, Model model) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex=request.getParameter("sex");
        Integer age=Integer.parseInt(request.getParameter("age"));

        return "login";//视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
    }


    @RequestMapping(value = "/reg2",method = RequestMethod.GET) //必须是get
    public String reg2(String name, String password, String sex,
                       Integer age, Model model) {

        return "login";//视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
    }


    @RequestMapping(value = "/reg3",method = RequestMethod.GET) //必须是get
    public String reg3(@RequestParam(required = true)String name,
                       @RequestParam(required = false,defaultValue = "18") Integer age) {
        System.out.println(name);
        return "login";//视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
    }


    @RequestMapping(value = "/reg4",method = RequestMethod.GET) //必须是get
    public String reg4(User user) {
        System.out.println(user.getName());
        return "login";//视图名称 =》跳转到 /WEB-INF/jsp/ + login + .jsp
    }

    @RequestMapping("/page")
    public  String page(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                        @RequestParam(required = false,defaultValue = "5") Integer pageSize){
        return "login";
    }
}
